package com.sugar.real.estate.module.business.service.userbuildingconsult;

import cn.hutool.core.collection.CollUtil;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.module.business.controller.admin.userbuildingconsult.vo.UserBuildingConsultPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.userbuildingconsult.vo.UserBuildingConsultRespVO;
import com.sugar.real.estate.module.business.controller.app.userbuildingconsult.vo.UserBuildingConsultSaveReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.userbuildingconsult.UserBuildingConsultDO;
import com.sugar.real.estate.module.business.dal.mysql.userbuildingconsult.UserBuildingConsultMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.sugar.real.estate.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.real.estate.module.business.enums.ErrorCodeConstants.USER_BUILDING_CONSULT_NOT_EXISTS;

/**
 * 用户楼盘咨询 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class UserBuildingConsultServiceImpl implements UserBuildingConsultService {

    @Resource
    private UserBuildingConsultMapper userBuildingConsultMapper;

    @Override
    public void createUserBuildingConsult(UserBuildingConsultSaveReqVO createReqVO, Long userId) {
        // 插入
        UserBuildingConsultDO userBuildingConsult = BeanUtils.toBean(createReqVO, UserBuildingConsultDO.class);
        userBuildingConsult.setUserId(userId);
        userBuildingConsultMapper.insert(userBuildingConsult);
    }

    @Override
    public void deleteUserBuildingConsult(Long id) {
        // 校验存在
        validateUserBuildingConsultExists(id);
        // 删除
        userBuildingConsultMapper.deleteById(id);
    }

    @Override
        public void deleteUserBuildingConsultListByIds(List<Long> ids) {
        // 校验存在
        validateUserBuildingConsultExists(ids);
        // 删除
        userBuildingConsultMapper.deleteByIds(ids);
        }

    private void validateUserBuildingConsultExists(List<Long> ids) {
        List<UserBuildingConsultDO> list = userBuildingConsultMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(USER_BUILDING_CONSULT_NOT_EXISTS);
        }
    }

    private void validateUserBuildingConsultExists(Long id) {
        if (userBuildingConsultMapper.selectById(id) == null) {
            throw exception(USER_BUILDING_CONSULT_NOT_EXISTS);
        }
    }

    @Override
    public UserBuildingConsultDO getUserBuildingConsult(Long id) {
        return userBuildingConsultMapper.selectById(id);
    }

    @Override
    public PageResult<UserBuildingConsultRespVO> getUserBuildingConsultPage(UserBuildingConsultPageReqVO reqVO) {

        LocalDateTime[] createTime = reqVO.getCreateTime();
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        if (Objects.nonNull(createTime) && createTime.length == 2) {
            startTime = createTime[0];
            endTime = createTime[1];
        }
        Long count = userBuildingConsultMapper.selectPageCount(reqVO.getUserId(), reqVO.getFollowId(),
                reqVO.getType(), startTime, endTime, reqVO.getName(), reqVO.getMobile());
        if (count > 0) {
            Integer pageNo = reqVO.getPageNo();
            Integer pageSize = reqVO.getPageSize();

            int skipCount = (pageNo - 1) * pageSize;

            List<UserBuildingConsultRespVO> dataList = userBuildingConsultMapper.selectCustomPage(reqVO.getUserId(),
                    reqVO.getFollowId(), reqVO.getType(),
                    startTime, endTime,
                    reqVO.getName(), reqVO.getMobile(),
                    skipCount, pageSize);

            return new PageResult<>(dataList, count);
        }
        return new PageResult<>(Collections.emptyList(), 0L);
    }

}